﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProTrack.Api.Helper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IServices.Examine;
using ProTrack.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProTrack.Api.Controllers
{
    /// <summary>
    /// 检测项设置服务接口
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class QcProjectController : ControllerBase
    {
        public QcProjectController(IQcProjectServices services, LogHelper LogHelper) 
        {
            Services = services;
            this.LogHelper = LogHelper;
        }
        public IQcProjectServices Services { get; }
        public LogHelper LogHelper { get; }

        /// <summary>
        /// 获取所有检测项设置
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll()
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var list = await Services.GetAllAsync();
                status.Data = list;
                status.Code = list.Count();
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试";
                await LogHelper.WriteErrorLog("检测项设置-获取所有", ex, HttpContext);
            }
            return Ok(status);
        }
        /// <summary>
        /// 分页获取检测项设置
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="ProjectCode"></param>
        /// <param name="ProjectName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get([FromQuery] int PageIndex, [FromQuery] int PageSize, [FromQuery] string ProjectCode, [FromQuery] string ProjectName)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                ProjectCode = ProjectCode ?? "";
                ProjectName = ProjectName ?? "";
                var list = await Services.GetAsync(PageIndex, PageSize, ProjectCode, ProjectName);
                status.Data = list;
                status.Count = await Services.GetCountAsync(ProjectCode, ProjectName);
            }
            catch (Exception ex)
            {

                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                await LogHelper.WriteErrorLog("检测项设置-分页获取", ex,HttpContext);
            }
            return Ok(status);
        }
        /// <summary>
        /// 新增检测项设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] QcProject model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                model.CreateTime = StringHelper.GetTimeNow();
                model.Creator = User.Identity.Name;
                var isSuccess = await Services.AddAsync(model);
                if (isSuccess)
                {
                    status.Msg = "新增成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "新增失败，请检查数据";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                await LogHelper.WriteErrorLog("检测项设置-新增", ex, HttpContext);
            }
            return Ok(status);
        }
        /// <summary>
        /// 修改检测项设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Updata([FromForm] QcProject model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                model.UpdateTime = StringHelper.GetTimeNow();
                model.Updater = User.Identity.Name;
                var isSuccess = await Services.UpdateAsync(model);
                if (isSuccess)
                {
                    status.Msg = "修改成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "修改失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {

                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                await LogHelper.WriteErrorLog("检测项设置-修改", ex, HttpContext);
            }
            return Ok(status);
        }
        /// <summary>
        /// 批量删除检测项设置
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll([FromForm] string Ids)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var arr = Ids.SplitId();
                var isSuccess = await Services.DeleteAsync(arr);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";   
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                await LogHelper.WriteErrorLog("检测项设置", ex, HttpContext);    
            }
            return Ok(status);
        }

        [HttpDelete]
        public async Task<IActionResult> Del ([FromForm] int Id)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var isSuccess = await Services.DeleteAsync(Id);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                await LogHelper.WriteErrorLog("检测项设置", ex, HttpContext);
            }
            return Ok(status);
        }
    }
}
